<?php
include_once("functions/db_manipulate.php");

if (!init()) {
    redirect("login.php");
} else if (!isConversationMember()) {
    exit(); //Page with error and redirection
}

checkIfDialogRead($_GET['did']);


$messagesResultSet = getConversation($_GET['did']);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Диалог</title>
    <link rel="stylesheet" href="css/navigation.css">
    <link rel="stylesheet" type="text/css" href="css/news.css">
    <link rel="stylesheet" type="text/css" href="css/square.css">
    <link rel="stylesheet" type="text/css" href="css/comment.css">
    <link rel="stylesheet" type="text/css" href="css/menu.css">
    <link rel="stylesheet" type="text/css" href="css/common.css">
    <link rel="stylesheet" type="text/css" href="css/messages.css">
    <script src="resources/jquery.js"></script>

    <meta charset="UTF-8">

<body class="body">
<?php
include_once("menu.php");
?>

<div id="square">

    <div class="messagesdiv">
        <div class="messagesheader">
            Ваш диалог с пользователем '<? getDialogPartner($_GET['did']) ?>':
        </div>

        <?php
        while ($message = mysql_fetch_assoc($messagesResultSet)) {

            if ($message['uid'] == $_SESSION['id']) {
                $header = "firstmsgheader";
            } else {
                $header = "secondmsgheader";
            }

            ?>
            <div class="msg">
                <?
                if ($message['uid'] == $_SESSION['id']) {
                    ?>
                    <div class="deletemessagebutton" onclick="$('#deletediv<?= $message['id'] ?>').slideDown('slow')">
                        x
                    </div>
                <?
                }
                ?>
                <div class="<?= $header ?>">
                    <?= $message['author'] ?> (<?= $message['date'] ?> <?= $message['time'] ?>):
                </div>
                <div class="msgcontent">
                    <?= htmlspecialchars($message['content']) ?>
                </div>
            </div>

            <div class="deletedialogdiv" id="deletediv<?= $message['id'] ?>">
                Вы уверены, что хотите удалить данное сообщение ?
                (После удаления восстановить его будет невозможно)
                <div class="askdeletediv">
                    <div class="deletebutton"
                         onclick="location.href='message_delete_handler.php?mid=<?= $message['id'] ?>'">
                        Да
                    </div>
                    <div class="cancelbutton" onclick="$('#deletediv<?= $message['id'] ?>').slideUp('slow')">
                        Отмена
                    </div>
                </div>
            </div>
        <?php
        }
        ?>
    </div>

    <form action="message_add_handler.php" method="post" id="addform">

        <div class="common">
            <span style="font-weight: bold">Новое сообщение:</span>

            <textarea id="textarea" name="content" rows="11" placeholder="Введите текст Вашего сообщения.."></textarea>

            <div id="commentbutton" style="margin-left: 520px" onclick="$('#addform').submit()">
                Отправить
            </div>

            <input type="hidden" name="did" value="<?= $_GET['did'] ?>">
        </div>
    </form>

</div>

<?php
include_once("navigation.html");
?>
</body>
</html>

</head>

